package com.jwt.controller;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.jwt.pojo.User;
import com.jwt.serivce.UserSerivce;
import com.jwt.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class UserControoler {
   @Autowired
    private UserSerivce userSerivce;

   @GetMapping("/user/login")
    private Map<String,String> login(String username, String password, HttpServletRequest request){
       HashMap<String, String> map = new HashMap<>();
       HashMap<String, String> pload = new HashMap<>();
       log.info("用户名: "+username);
        log.info("密码: "+password);
       try {
           User user = userSerivce.findBy(new User(username, password));
            //生成token
           String token = JWTUtils.getToken(pload);
           map.put("token",token);
           map.put("msg", "登录成功");
           return map;
       } catch (Exception e) {
            map.put("user","认真失败");
       }
        return map;
   }
    @GetMapping("/token/s")
    public  HashMap<String, Object>  test(String token){
        log.info("当前的token :"+token);
        HashMap<String, Object> map = new HashMap<>();
           DecodedJWT verify = JWTUtils.verify(token);
            map.put("status","成功");
       return map;
    }
    @GetMapping("/token/ss")
    public  HashMap<String, Object>  test1(String token){
        HashMap<String, Object> map = new HashMap<>();
        log.info("当前的token :"+token);
        map.put("status","成功");
        return map;
    }
}
